﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FisheryPlatform.Entity;
using FisheryPlatform.Bussiness.Product;
using FisheryPlatform.Bussiness.ShareCode;

namespace FisheryPlatform.Web.Admin.Product
{
    public partial class Index : System.Web.UI.Page
    {
        protected List<ProductEntity> ProList = null;
        protected int pageIndex = 0;
        protected int pageSize = 25;
        protected int pageCount = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            InitData();
        }

        private void InitData()
        {
            #region
            if (!int.TryParse(Request.QueryString["pageIndex"], out pageIndex))
            {
                pageIndex = 1;
            }
            this.txt_PageIndex.Value = pageIndex.ToString();
            this.txt_StartDate.Value = Request.QueryString["dateStart"];
            this.txt_EndDate.Value = Request.QueryString["dateEnd"];
            this.txt_Title.Value = Request.QueryString["title"];
            long cateId = 0;
            if (long.TryParse(Request.QueryString["cateId"], out cateId) && cateId > 0)
            {
                this.pl_Cate.SelectedItemIds = Request.QueryString["cateId"];
            }
            var status = int.Parse(Request.QueryString["status"] ?? "-1");
            switch ((ProductStatusEnum)status)
            {
                case ProductStatusEnum.Draft: this.sel_Status.SelectedIndex = 1; break;
                case ProductStatusEnum.Publishing: this.sel_Status.SelectedIndex = 2; break;
                case ProductStatusEnum.Pausing: this.sel_Status.SelectedIndex = 3; break;
                default: this.sel_Status.SelectedIndex = 0; break;
            }
            this.txt_ProCode.Value = Request.QueryString["proCode"];
            #endregion
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            #region
            var sqlWhereString = "";
            GetSearchCondition(out sqlWhereString);
            ProList = ProductManager.Search(sqlWhereString, pageIndex, pageSize, out pageCount);

            this.pb_PageBar.PbModel.CurrentIndex = pageIndex;
            this.pb_PageBar.PbModel.PageCount = pageCount;
            this.pb_PageBar.PbModel.PageSize = pageSize;
            this.pb_PageBar.PbModel.IsForAjax = true;
            this.pb_PageBar.PbModel.AjaxPageClickCallback = "PageChanged({index})";
            #endregion
        }

        private void GetSearchCondition(out string sqlWhereString)
        {
            #region
            sqlWhereString = " 1=1 ";
            if (string.IsNullOrEmpty(txt_ProCode.Value) == false)
            {
                sqlWhereString += " and ProductId=" + txt_ProCode.Value;
            }
            else
            {
                if (!string.IsNullOrEmpty(this.txt_Title.Value))
                {
                    sqlWhereString += " and Title like '" + this.txt_Title.Value.Trim() + "%'";
                }
                DateTime dateStart = DateTime.Now;
                if (DateTime.TryParse(Request.QueryString["DateStart"], out dateStart))
                {
                    sqlWhereString += " and CreateDate>='" + dateStart.ToString("yyyy-MM-dd") + "'";
                }

                DateTime dateEnd = DateTime.Now;
                if (DateTime.TryParse(Request.QueryString["DateEnd"], out dateEnd))
                {
                    sqlWhereString += " and CreateDate<='" + dateEnd.ToString("yyyy-MM-dd") + "'";
                }

                long cateId = 0;
                if (long.TryParse(this.pl_Cate.SelectedItemIds, out cateId))
                {
                    sqlWhereString += " and CategoryId=" + this.pl_Cate.SelectedItemIds;
                }
                int status = -1;
                if (int.TryParse(Request.QueryString["status"], out status) && status > -1)
                {
                    sqlWhereString += " and status = " + status;
                }
            }
            #endregion
        }

        protected string GetProductStatus(ProductStatusEnum status)
        {
            return ProductManager.GetProductStatusText(status);
        }
    }
}